#include "server.h"

//设某位为1
void set_bit(uint32_t *bitmap, int pos) {
    bitmap[pos / WORD_SIZE] |= (1U << (pos % WORD_SIZE));
}

//设某位为0
void clear_bit(uint32_t *bitmap, int pos) {
    bitmap[pos / WORD_SIZE] &= ~(1U << (pos % WORD_SIZE));
}

//清空位图，并把在位图内netfd关闭
void find_set_bits_fast(uint32_t bitmap[][ARRAY_SIZE],int epfd,int cur) {
    for (int i = 0; i < (BITMAP_SIZE + WORD_SIZE - 1) / WORD_SIZE; i++) {
        uint32_t word = bitmap[cur][i];
        while (word != 0) {
            // 找到最低位的1的位置
            int bit_pos = __builtin_ctz(word);  // Count Trailing Zeros
            int netfd = i * WORD_SIZE + bit_pos;
            printf("netfd = %d is kicked out.\n",netfd);
            for(int j = 0; j < TIMEOUT; ++j){
                clear_bit(bitmap[j],netfd);
            }
            epollDel(epfd,netfd);
            close(netfd);
            // 清除已处理的1
            word ^= (1U << bit_pos);
        }
    }
}
